<template>
  <div class="sub-ategory">
    <div class="container">
      <!-- 面包屑 -->
    <SubBread></SubBread>
    <!-- 筛选区 -->
    <SubFilter @filter-change="filterChange"></SubFilter>
    <div class="goods-list">
      <!-- 排序区 -->
      <SubSort @sort-change="sortChange"></SubSort>
      <!-- 列表区 -->
      <ul>
          <li v-for="item in goodList" :key="item.id" >
            <GoodsItem :sub="item" />
          </li>
        </ul>
      <XtxIfiniteLoading :loading="loading" :finished="finished" @infinite="getloadingData"></XtxIfiniteLoading>
    </div>

    <!-- 测试复选框
    <XtxCheckbox v-model="isAllChecked">全选</XtxCheckbox> -->
    </div>
  </div>
</template>

<script>
import SubBread from './sub-bread.vue'
import SubFilter from './sub-filter.vue'
import SubSort from './sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { reactive, ref, watch } from 'vue'
import { fintCategoryList } from '@/api/category'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem
  },
  setup () {
    // const isAllChecked = ref(false)
    const route = useRoute()
    // 加载中

    const loading = ref(false)
    // 是否加载完毕
    const finished = ref(false)

    // 商品列表数据
    const goodList = ref([])
    // 请求参数
    const index = ref(1)
    const reqParams = reactive(
      {
        page: index,
        pageSize: 20
      }

    )
    const getloadingData = () => {
      loading.value = true
      // 设置二级分类的ID
      reqParams.categoryId = route.params.id
      fintCategoryList(reqParams).then(({ result }) => {
        // 获取数据成功
        // 有数据就追加数据
        if (result.items.length) {
          goodList.value.push(...result.items)
          index.value++
          loading.value = false
        } else {
          // 没有数据加载完成
          finished.value = true
          loading.value = false
        }
      })
    }
    // 更改了二级分类应该重新加载数据
    watch(() => route.params.id, (newVal) => {
      if (newVal && `/category/sub/${newVal}` === route.path) {
        finished.value = false
        goodList.value = []
        index.value = 1
      }
    })

    // 1.更改排序组建的筛选属性,重新请求
    const sortChange = (sortParams) => {
      // console.log(sortParams)
      finished.value = false
      // 合并参数,保留之前参数
      goodList.value = []
      index.value = 1
      reqParams.value = { ...reqParams, ...sortParams }
    }
    // 2.更改筛选条件的筛选数据，重新请求
    const filterChange = (filterParams) => {
      // console.log(filterParams)
      finished.value = false
      goodList.value = []
      index.value = 1
      reqParams.value = { ...reqParams, ...filterParams }
    }
    return { loading, finished, getloadingData, goodList, sortChange, filterChange }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background-color: #fff;
  padding: 0 25px;
  margin-top: 25px;
   ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
